package profile;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import model.ProfileModel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import util.ProfileChartHelper;
import view.IView;
import controller.ProfileController;

/**
 * This is the main profile result frame that shows the graph of 
 * a profile, along with some statistics and the threshold selection
 * on the side.  
 * 
 */
public class ProfileResultFrame extends JFrame implements IView {

    private static final long serialVersionUID = 965634871039425317L;
    
    private Profile profile;
    private ProfileController controller;
    private JPanel profilePanel;
    private String imageFilename;
    private int currentMad = 3;
    
    
    private JPanel leftPanel = new JPanel();
    private JFreeChart profileChart;

    public ProfileResultFrame(Profile profile, ProfileController controller, ProfileModel model, String filename) {
        this.profile = profile;
        this.controller = controller;
        this.imageFilename = filename;
        
        controller.addView(this);
        controller.addModel(model);
        
        initComponents();
        
        this.setSize(new Dimension(1024, 600));
    }

    private void initComponents() {
        initLeftPanel();
        
        this.setLayout(new BorderLayout());
        this.add(leftPanel, BorderLayout.WEST);
        
        setProfilePanel();
    }
    
    private void initLeftPanel() {
        leftPanel.add(new ProfileResultLeftPanel(controller, profile, 3, imageFilename, this));
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if(evt.getPropertyName().equals(ProfileController.PROFILE_MAD_PROPERTY)) {
            currentMad = (Integer) evt.getNewValue();
            setProfilePanel();
        }
    }

    /**
     * Adds the chart to the main profilePanel.  
     */
    private void setProfilePanel() {
        if(profilePanel != null) {
            this.remove(profilePanel);
        }
        
        profileChart = ProfileChartHelper.getProfileChart(profile, "Profile", currentMad);
        int panelWidth = 800; int panelHeight = 600;
        if(profilePanel != null) {
            panelWidth = profilePanel.getWidth();
            panelHeight = profilePanel.getHeight();
        }
        profilePanel = new ChartPanel(profileChart, panelWidth, panelHeight,
                1600, 1200, 400, 300, 
                false, //enable buffer (in my testing, enabling this made it noticeably slower)
                true, //enable chart property editor via right click 
                true, //enable save options via right click 
                false, //enable print option via right click 
                true, //enable zoom options via right click
                true); //enables tooltips for chart

        profilePanel.setBounds(leftPanel.getWidth(), 0, panelWidth, panelHeight);
        this.add(profilePanel, BorderLayout.CENTER);
        this.repaint();
    }
    
    public JFreeChart getProfileChart() {
        return profileChart;
    }

}
